package com.usf.jprovider.schema.provider.dao.resultset;

import java.sql.SQLException;

import javax.xml.bind.annotation.XmlAttribute;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.tools.DAOTools;

public final class DAOField extends AbstractDAOResultSetSource {
	
	@XmlAttribute protected String table;
	@XmlAttribute protected String column;
	
	public DAOField(){
		super();
		table = "";
		column = "";
	}
	public String getTable() {
		return table;
	}
	public String getColumn() {
		return column;
	}

	@Override
	protected String createUnformated(Environment env, JObjectNode node) throws SQLException {
		String query = entrySelector.selectField(env, table, column);
		String values = DAOTools.executeSelectQuery(env.getConnection(), query);
		return valueSelector.create(env, node, values);
	}
	
	@Override
	public boolean checkValue(Environment env, JObjectNode node, JType<?> object) throws SQLException {
		String queryValue = valueSelector.toQuery(object.toString());
		String query = entrySelector.selectField(env, table, column, queryValue);
		String values = DAOTools.executeSelectQuery(env.getConnection(), query);
		return valueSelector.check(env, node, values, object.toString());
	}
}
